package com.lyf.network.practice.nio;

import org.junit.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;

/**
 * @author LYF
 * @date 2022/6/24
 */
public class ChannelTests {
    /*
    通道--

     */
    @Test
    public void testChannelBase(){
        // interface Channel extends Closeable
        //Channel channel = // FileChannel,SocketChannel,...

        String path = "E:\\JavaProjects\\LearnProjects\\learn-practice\\java-base\\src\\main\\java\\com\\lyf\\network\\practice\\nio\\files\\tmp.txt";
        try {
            /*
            public abstract class FileChannel
    extends AbstractInterruptibleChannel
    implements SeekableByteChannel, GatheringByteChannel, ScatteringByteChannel
             */

            FileChannel fileChannel = FileChannel.open(Path.of(path));
            ByteBuffer buffer = ByteBuffer.allocate(10);
            //fileChannel.write(buffer);

            fileChannel.read(buffer);

            buffer.position(0);
            while(buffer.hasRemaining())
            {
                System.out.println((char) buffer.get());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**archived*/
    @Test
    public void testSocketChannel() throws IOException, InterruptedException {

        //SocketChannel socketChannel = SocketChannel.open();
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("waiting connect...");
        Socket socket = serverSocket.accept();
        SocketChannel socketChannel = socket.getChannel();
        socketChannel.configureBlocking(false);
        System.out.println("connected...");
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        // 循环监听读取情况
        while (true){
            buffer.position(0);
            socketChannel.read(buffer);

            byte[] dst =new byte[1024];
            buffer.get(dst);
            System.out.println(new String(dst));

            TimeUnit.SECONDS.sleep(2);
        }

    }

    /**archived*/
    @Test
    public void testCon() throws IOException {
        Socket socket = new Socket("127.0.0.1",9999);
        SocketChannel socketChannel = socket.getChannel();
        socketChannel.configureBlocking(false);

        ByteBuffer buffer =ByteBuffer.allocate(10);
        buffer.put("hello".getBytes());
        socketChannel.write(buffer);


    }


    @Test
    public void serverChannel() throws IOException, InterruptedException {
        ServerSocketChannel socketChannel = ServerSocketChannel.open();
        socketChannel.socket().bind(new InetSocketAddress(9999));
        socketChannel.configureBlocking(false);
        System.out.println("waiting...connect...");
        while (true){
            SocketChannel acceptChannel = socketChannel.accept();
            if(acceptChannel==null){
                TimeUnit.SECONDS.sleep(2);
                continue;
            }
            ByteBuffer buffer = ByteBuffer.allocate(1024);
            acceptChannel.read(buffer);
            byte[]dst = new byte[1024];
            buffer.get(dst);
            System.out.println(new String(dst));
        }
    }

    @Test
    public void clientChannel() throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1",9999));
        socketChannel.configureBlocking(false);

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        buffer.put("hello".getBytes());
        socketChannel.write(buffer);
    }

}
